/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validation;

import bean.Kreditgeber;
import bean.Kunde;
import java.math.BigInteger;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


public class KreditCheckValidator implements ConstraintValidator<KreditCheck, Kunde> {

    @Override
    public void initialize(KreditCheck a) {
    }

    @Override
    public boolean isValid(Kunde t, ConstraintValidatorContext cvc) {
        BigInteger actKredit = new BigInteger("0");
        
        for(Kreditgeber kg : t.getKredite()) {
            actKredit = actKredit.add(kg.getSumme());
        }
        
        if(actKredit.compareTo(t.getMaxKredit()) > 0) {
            t.getKredite().remove(t.getKredite().size()-1); //Änderung rückgängig machen
            return false;
        }
        
        return true;
    }
    
}
